/*
* ============================================================================
*  Name        : sea_platform.cpp
*  Part of     : Finite state machine framework 
*  Description : 
*  Version     : %version: %
*  
*
* ============================================================================
*/

#include "sea_platform.h"

/**
 * @file sea_platform.cpp
 * @brief
 *  Common platform classes implementation
 *
 * @ingroup Platforms
 */
using namespace sea;

// -------------------------------------------------------------------------------------------------
// Ctor
// -------------------------------------------------------------------------------------------------
OsNotifier::OsNotifier( ) : 
    m_waiting( false )
{
    m_semaphore = NEW OsSemaphore;
}

// -------------------------------------------------------------------------------------------------
// Dtor
// -------------------------------------------------------------------------------------------------
OsNotifier::~OsNotifier( )
{
    if ( m_semaphore )
    {
        m_semaphore->signal( );
        
        // Delete the semaphore myself, 
        // b/c no one is waiting on it
        if ( !m_waiting )
        {
            delete m_semaphore;
        }
    }
}

// -------------------------------------------------------------------------------------------------
// Reserve a "job" for execution
// -------------------------------------------------------------------------------------------------
void OsNotifier::reserve( )
{
    if ( m_semaphore )
    {
        m_waiting = true;
        get_ref( );
    }
}

// -------------------------------------------------------------------------------------------------
// "Job" is done
// -------------------------------------------------------------------------------------------------
void OsNotifier::release( )
{
    if ( m_semaphore )
    {
        release_ref( );
    }
}

// -------------------------------------------------------------------------------------------------
// Wait for all to finish
// -------------------------------------------------------------------------------------------------
void OsNotifier::wait( )
{
    // Prepare to kill this object
    OsSemaphore* local = m_semaphore;
    release_ref( );

    if ( local )
    {
        local->wait( );
        local->release_ref( );
    }
}

// -------------------------------------------------------------------------------------------------

